﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using C1.WPF;
using C1.WPF.Docking;
using Model.Modules;
//using C1.WPF.Theming;
using SteelDetection.UIControl;
using SteelDetection.Utility;
using Utility;
using ViewModel;
using Proxy;


namespace SteelDetection
{

    public partial class PageMain : Page
    {
        List<UCRulerchart> charts;
        public PageMain()
        {
            InitializeComponent();

            this.Loaded += PageMain_Loaded;
            this.Focus();

            this.SetBinding(SteelDefectlistProperty, "SteelDefectlistView");
            this.SetBinding(CurrentBatchProperty, "CurrentBatch");
            this.SetBinding(CurrentLeftEdgeProperty, "CurrentLeftEdge");
            this.SetBinding(CurrentSteelDefectProperty, new Binding("CurrentSteelDefect") { Mode = BindingMode.TwoWay });
            this.SetBinding(ShowFloatViewerProperty, new Binding("ShowFloatViewer") { Mode = BindingMode.TwoWay });
            radochart.AllChartLoaded += (charts) =>
            {
                this.charts = charts;
                radochart.MoveCurrentLeftEdge(CurrentLeftEdge);
            };
            radochart.CurrentSteelDefectChanged += (steel_defect) =>
            {
                if (steel_defect == null)
                    floatviewer.Visibility = Visibility.Collapsed;
                else
                {
                    radochart.ChangeCurrentSteelDefectAnno(steel_defect);
                    this.SetValue(CurrentSteelDefectProperty, steel_defect);
                    floatviewer.Visibility = Visibility.Visible;

                    Keyboard.Focus(this);
                }
            };
            typechart.Series[0].SymbolRendering += PageMain_SymbolRendering;
        }

        private void PageMain_Loaded(object sender, RoutedEventArgs e)
        {

            SteelDefectlist.CollectionChanged += SteelDefectlist_CollectionChanged; ;
            foreach (var steel_Defects in SteelDefectlist)
            {
                UpdateRectAnnos(steel_Defects);
            }

            this.KeyDown += (s, ee) =>
            {
                if (ee.Key == Key.Escape)
                {
                    floatviewer.Visibility = Visibility.Collapsed;
                }
            };
            Logger.Info("PageMainLoaded");
            Loaded -= PageMain_Loaded;
        }

        private void SteelDefectlist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (var item in e.NewItems)
                {

                    UpdateRectAnnos(item as ObservableCollection<steel_defect>);
                }

            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
            {
                radochart.ClearAnnos();
            }
        }

        public ObservableCollection<ObservableCollection<steel_defect>> SteelDefectlist
        {
            get { return (ObservableCollection<ObservableCollection<steel_defect>>)GetValue(SteelDefectlistProperty); }
            set { SetValue(SteelDefectlistProperty, value); }
        }
        public static readonly DependencyProperty SteelDefectlistProperty = DependencyProperty.RegisterAttached("SteelDefectlist", typeof(ObservableCollection<ObservableCollection<steel_defect>>), typeof(PageMain), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((d, e) =>
        {
            if (e.NewValue != null)
            {
                var pagemain = d as PageMain;
                pagemain.radochart.ClearAnnos();
                Logger.Info("客户端缺陷数量：" + (e.NewValue as ObservableCollection<ObservableCollection<steel_defect>>).Count().ToString());
                foreach (var item in e.NewValue as ObservableCollection<ObservableCollection<steel_defect>>)
                {
                    pagemain.UpdateRectAnnos(item as ObservableCollection<steel_defect>);
                }
            }
        })));


        public steel_defect CurrentSteelDefect
        {
            get { return (steel_defect)GetValue(CurrentSteelDefectProperty); }
            set { SetValue(CurrentSteelDefectProperty, value); }
        }
        public static readonly DependencyProperty CurrentSteelDefectProperty = DependencyProperty.RegisterAttached("CurrentSteelDefect", typeof(steel_defect), typeof(PageMain), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((d, e) =>
        {
            if (e.NewValue != null)
            {
            }
        })));


        public batch CurrentBatch
        {
            get { return (batch)GetValue(CurrentBatchProperty); }
            set { SetValue(CurrentBatchProperty, value); }
        }
        public static readonly DependencyProperty CurrentBatchProperty = DependencyProperty.RegisterAttached("Curren", typeof(batch), typeof(PageMain), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((d, e) =>
        {
            if (e.NewValue != null && e.OldValue != null)
            {
                (d as PageMain).radochart.ClearAnnos();
            }
        })));


        public int CurrentLeftEdge
        {
            get { return (int)GetValue(CurrentLeftEdgeProperty); }
            set { SetValue(CurrentLeftEdgeProperty, value); }
        }
        public static readonly DependencyProperty CurrentLeftEdgeProperty = DependencyProperty.RegisterAttached("CurrentLeftEdge", typeof(int), typeof(PageMain), new FrameworkPropertyMetadata(0, new PropertyChangedCallback((d, e) =>
        {
            if (e.NewValue != null)
            {
                (d as PageMain).MoveCurrentLeftEdge();
            }
        })));

        public bool ShowFloatViewer
        {
            get { return (bool)GetValue(ShowFloatViewerProperty); }
            set { SetValue(ShowFloatViewerProperty, value); }
        }
       
        public static readonly DependencyProperty ShowFloatViewerProperty = DependencyProperty.RegisterAttached("ShowFloatViewerProperty", typeof(bool), typeof(PageMain), new FrameworkPropertyMetadata(false, new PropertyChangedCallback((d, e) =>
        {
            if (e.NewValue != null && (bool)e.NewValue)
            {
                (d as PageMain).floatviewer.Visibility = Visibility.Visible;
                (d as PageMain).ShowFloatViewer = false;
            }
        })));



        public void UpdateRectAnnos(ObservableCollection<steel_defect> defectinfos)
        {
            radochart.UpdateRectAnnos(defectinfos);
        }


        public void MoveCurrentLeftEdge()
        {
            if ((this.DataContext as VM_PageMain).LiveIsRefreshing)
                radochart.MoveCurrentLeftEdge(CurrentLeftEdge);
        }


        private void olimagelistbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            floatviewer.Visibility = Visibility.Visible;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            floatviewer.Visibility = Visibility.Collapsed;
        }


        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void SelectAllFilter_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void SelectAllFilter_Unchecked(object sender, RoutedEventArgs e)
        {

        }



        private void PageMain_SymbolRendering(object sender, C1.WPF.Chart.RenderSymbolEventArgs e)
        {
            defect_type defecttype = StaticData.DefectTypes.Where(t => t.id == (e.Item as defectsubtotalbytype).Type).FirstOrDefault();
            if (defecttype != null)
            {
                e.Engine.SetFill(new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(defecttype.color)));
            }
        }
    }






}
